import java.io.*;
import java.util.Scanner;

public class Demo16 {
    public static void main(String[] args) throws IOException {
        System.out.println("请输入复制的文件路径");
        Scanner scanner = new Scanner(System.in);
        String src = scanner.next();
        File srcFile = new File(src);
        if (!srcFile.isFile()) {
            System.out.println("源文件不存在或者不是一个文件");
            return;
        }
        System.out.println("请输入复制到目标路径");
        String dest = scanner.next();
        File destFile = new File(dest);
        //不要求目标文件本身存在，但要保证目标文件所在的目录所在。
        //假设目标文件写作d:/tmp/cat2.jpg,就需保证d:tmp目录所在
        if (!destFile.getParentFile().isDirectory()) {
            System.out.println("您的路径非法");
            return;
        }
        try (InputStream inputStream = new FileInputStream(srcFile);
            OutputStream outputStream = new FileOutputStream(destFile)) {
            while(true)
                {
                    byte[] buffer = new byte[1024];
                    int n = inputStream.read(buffer);
                    System.out.println("n=" + n);
                    if (n == -1) {
                        System.out.println("读到eof,结束");
                        break;
                    }
                    outputStream.write(buffer, 0, n);
                }
            }
        }
    }
